package com.xukang.qd.listener;

import com.ibm.jms.JMSBytesMessage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.jms.BytesMessage;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;

public class ConsumerIbmMqMessageListener implements MessageListener {

    public static Logger logger= LoggerFactory.getLogger(ConsumerIbmMqMessageListener.class);


    public void init(){
        logger.info("ibmmq 消费队列监听开启");
    }
    public void destroy(){
        logger.info("ibmmq 消费队列监听销毁");
    }

    @Override
    public void onMessage(Message message) {

        try {
            System.out.println(message.getClass().getName());
            if(message instanceof BytesMessage){
                JMSBytesMessage bytesMessage = (JMSBytesMessage) message ;
                logger.info("BodyLength:"+bytesMessage.getBodyLength());
                logger.info("JMSMessageID:"+bytesMessage.getJMSMessageID());
                logger.info("CorrelationID:"+bytesMessage.getJMSCorrelationID());
                logger.info("JMSType:"+bytesMessage.getJMSType());
                byte[] bys = null;
                bys = new byte[(int) bytesMessage.getBodyLength()];
                bytesMessage.readBytes(bys);
                String msg = new String(bys);
                logger.info("message:"+msg);

            }else {
                logger.info(message.getClass().getName());
                TextMessage msg = (TextMessage) message;
                String msgStr = msg.getText();
                logger.info(" message消息内容:" + msgStr);
                logger.info("JMSType:"+msg.getJMSType());
                logger.info("CorrelationID:"+msg.getJMSCorrelationID());
                logger.info("JMSMessageID:"+msg.getJMSMessageID());

            }

        } catch (Exception e) {
            logger.error("监听异常：{}",e);
        }
    }
}